// import { getCommentDetail } from '../../../../services/good/comments/fetchCommentDetail';
import { endOrder } from '../../../utils/request';
import Toast from 'tdesign-miniprogram/toast/index';
Page({
  data: {
    serviceRateValue: 1,
    goodRateValue: 1,
    conveyRateValue: 1,
    isAnonymous: false,
    uploadFiles: [],
    gridConfig: {
      width: 218,
      height: 218,
      column: 3,
    },
    isAllowedSubmit: false,
    imgUrl: '',
    title: '',
    goodsDetail: '',
    imageProps: {
      mode: 'aspectFit',
    },
    orderId: 0,
    serviceCount: null,
  },

  countHandle(e) {
    this.setData({
      serviceCount: e.detail.value
    })
  },

  onLoad(options) {
    this.setData({
      orderId: options.orderId,
      serviceCount: options.serviceCount,
    });
  },

  onRateChange(e) {
    const { value } = e?.detail;
    const item = e?.currentTarget?.dataset?.item;
    this.setData({ [item]: value }, () => {
      this.updateButtonStatus();
    });
  },

  onAnonymousChange(e) {
    const status = !!e?.detail?.checked;
    this.setData({ isAnonymous: status });
  },

  handleSuccess(e) {
    const { files } = e.detail;

    this.setData({
      uploadFiles: files,
    });
  },

  handleRemove(e) {
    const { index } = e.detail;
    const { uploadFiles } = this.data;
    uploadFiles.splice(index, 1);
    this.setData({
      uploadFiles,
    });
  },

  onTextAreaChange(e) {
    const value = e?.detail?.value;
    this.textAreaValue = value;
    this.updateButtonStatus();
  },

  updateButtonStatus() {
    const {
      serviceRateValue,
      goodRateValue,
      conveyRateValue,
      isAllowedSubmit,
    } = this.data;
    const { textAreaValue } = this;
    const temp =
      serviceRateValue && goodRateValue && conveyRateValue && textAreaValue;
    if (temp !== isAllowedSubmit) this.setData({ isAllowedSubmit: temp });
  },

  async onSubmitBtnClick() {
    console.log("onSubmitBtnClick", this.textAreaValue, this.data.goodRateValue);
    const orderId = this.data.orderId;
    const content = this.textAreaValue;
    const starLevel =  this.data.goodRateValue;
    const serviceCount =  this.data.serviceCount;
    if (!content) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请填写评论内容'
      });
      return ;
    }
    const res = await endOrder({
      id: orderId,
      content: content,
      score: starLevel,
      serviceCount:serviceCount
    })
    // const { isAllowedSubmit } = this.data;
    // if (!isAllowedSubmit) return;
    Toast({
      context: this,
      selector: '#t-toast',
      message: '评价提交成功',
      icon: 'check-circle',
    });
    wx.reLaunch({
      url: '/pages/order/order-detail/index?orderId=' + orderId,
    })
  },
});
